//
//  YSInterestGroupView.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/3.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSInterestGroupView.h"
#import "YSGroupReview.h"

#define kMarginLeft 12
#define kGroupNameFont 18
#define kGroupLeaderFont 12
#define kNewestHotFont 15
#define kTopicNumberHeight 15

#define kPostsViewHeight 34

@interface YSInterestGroupView()
@property (weak, nonatomic) UIView *roundView;
@property (weak, nonatomic)  UIImageView *backgroundImageView;
@property (weak, nonatomic)  UIImageView *avatarView;
@property (weak, nonatomic)  UILabel *groupNameLabel;
@property (weak, nonatomic)  UILabel *groupLeaderLabel;
@property (weak, nonatomic)  UILabel *newestHotLabel;
@property (weak, nonatomic)  UILabel *topicNumberLabel;
@property (weak, nonatomic)  UIView *followingLabel;

@end

@implementation YSInterestGroupView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIView *roundView = [[UIView alloc] init];
        [self addSubview:roundView];
        _roundView = roundView;
        roundView.layer.masksToBounds = YES;
        [roundView.layer setCornerRadius:5];
        
        UIImageView *backgroundImageView = [[UIImageView alloc] init];
        [roundView addSubview:backgroundImageView];
        _backgroundImageView = backgroundImageView;
        
        UIImageView *avatarView = [[UIImageView alloc] init];
        avatarView.layer.masksToBounds = YES;
        [avatarView.layer setCornerRadius:60/2];
        [roundView addSubview:avatarView];
        _avatarView = avatarView;
        
        UILabel *groupNameLabel = [[UILabel alloc] init];
        [groupNameLabel setTextColor:publicTextColor];
        [groupNameLabel setFont:SYS_FONT(kGroupNameFont)];
        [roundView addSubview:groupNameLabel];
        _groupNameLabel = groupNameLabel;
        
        UILabel *groupLeaderLabel = [[UILabel alloc] init];
        [groupLeaderLabel setTextColor:HEXColor(@"#999491")];
        [groupLeaderLabel setFont:SYS_FONT(kGroupLeaderFont)];
        [roundView addSubview:groupLeaderLabel];
        _groupLeaderLabel = groupLeaderLabel;
        
        UILabel *newestHotLabel = [[UILabel alloc] init];
        [newestHotLabel setTextColor:groupLeaderLabel.textColor];
        [newestHotLabel setFont:SYS_FONT(kNewestHotFont)];
        newestHotLabel.text = @"最新热帖";
        CGSize newesLabelSize = [newestHotLabel.text sizeWithFont:SYS_FONT(kNewestHotFont)];
        newestHotLabel.size = newesLabelSize;
        [roundView addSubview:newestHotLabel];
        _newestHotLabel = newestHotLabel;
        
        UILabel *topicNumberLabel = [[UILabel alloc] init];
        [topicNumberLabel setTextColor:ThemeColor];
        [topicNumberLabel setFont:SYS_FONT(kGroupLeaderFont)];
        [topicNumberLabel setTextAlignment:NSTextAlignmentCenter];
        [topicNumberLabel setHidden:YES];
        
        topicNumberLabel.layer.masksToBounds = YES;
        [topicNumberLabel.layer setCornerRadius:kTopicNumberHeight/2];
        [topicNumberLabel.layer setBorderColor:RGBColor(204, 204, 204).CGColor];
        [topicNumberLabel.layer setBackgroundColor:[ThemeColor CGColor]];
        [roundView addSubview:topicNumberLabel];
        _topicNumberLabel = topicNumberLabel;
        
        UILabel *followingLabel = [[UILabel alloc] init];
        [followingLabel setFont:SYS_FONT(11)];
        [followingLabel setTextColor:[UIColor whiteColor]];
        [followingLabel setTextAlignment:NSTextAlignmentCenter];
        followingLabel.backgroundColor  = [UIColor colorWithWhite:0.5 alpha:0.8];
        followingLabel.size = CGSizeMake(40, 19);
        followingLabel.text = @"已关注";
        followingLabel.layer.masksToBounds = YES;
        [followingLabel.layer setCornerRadius:5];
        [roundView addSubview:followingLabel];
        _followingLabel = followingLabel;
        
        self.layer.masksToBounds = NO;
        [self.layer setCornerRadius:5];
        
        [[self layer] setShadowOffset:CGSizeMake(0.1, 0.1)];
        [[self layer] setShadowRadius:1];
        [[self layer] setShadowOpacity:1];
        [[self layer] setShadowColor:[UIColor colorWithWhite:0.5 alpha:0.8].CGColor];
    }
    return self;
}

- (void)setInterestGroup:(YSInterestGroup *)interestGroup{
    [_backgroundImageView setImageURLStr:interestGroup.titlePage];
    [_avatarView setImageURLStr:interestGroup.userImg];
    
    _groupNameLabel.text = interestGroup.groupName;
    _groupLeaderLabel.text = [NSString stringWithFormat:@"(组长：%@)", interestGroup.groupLeader];
    
    if (interestGroup.topicNum) {
        _topicNumberLabel.hidden = NO;
        _topicNumberLabel.text = [NSString stringWithFormat:@"%ld个话题",interestGroup.topicNum];
    }else{
        _topicNumberLabel.hidden = YES;
    }
    
    _followingLabel.hidden = !interestGroup.isAttention;
    
    [self initHotPostsViewWithReviews:interestGroup.reviews];
    
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    //roundView
    self.roundView.frame = self.bounds;
    
    //backgroundImage
    self.backgroundImageView.frame = CGRectMake(0, 0, self.width, (SCREENW - 50) * 580 /650);
    
    //following
    self.followingLabel.x = self.width - kMarginLeft - self.followingLabel.size.width;
    self.followingLabel.y = 20;

    //avatar
    self.avatarView.size = CGSizeMake(60, 60);
    self.avatarView.center = CGPointMake(self.width/2, CGRectGetMaxY(self.backgroundImageView.frame));
    

    // groupName and groupLeader
    CGSize groupNameSize = [_groupNameLabel.text sizeWithFont:SYS_FONT(kGroupNameFont)];
    CGSize groupLeaderSize = [_groupLeaderLabel.text sizeWithFont:SYS_FONT(kGroupLeaderFont)];
    
    CGFloat groupNameAndLeaderWidth =  groupLeaderSize.width + groupNameSize.width + 10;
    
    if (groupNameAndLeaderWidth > self.width - kMarginLeft *2) {
        groupNameAndLeaderWidth = self.width - kMarginLeft *2;
    }
    _groupNameLabel.frame = CGRectMake(self.width/2 - groupNameAndLeaderWidth/2, CGRectGetMaxY(_avatarView.frame) + 15, groupNameSize.width, groupNameSize.height);
    
    _groupLeaderLabel.frame = CGRectMake(CGRectGetMaxX(_groupNameLabel.frame) + 10, CGRectGetMaxY(_groupNameLabel.frame) - groupLeaderSize.height, groupLeaderSize.width, groupLeaderSize.height);
    
    // newestHot
    _newestHotLabel.x = kMarginLeft;
    _newestHotLabel.y = CGRectGetMaxY(_groupNameLabel.frame) + 40;

    // topicNumber
    CGSize topicNumberSize = [_topicNumberLabel.text sizeWithFont:SYS_FONT(kGroupLeaderFont)];
    CGFloat topicNumberWidth = topicNumberSize.width + 5;
    _topicNumberLabel.frame = CGRectMake(self.width - topicNumberWidth - kMarginLeft , CGRectGetMinY(_newestHotLabel.frame), topicNumberWidth, kTopicNumberHeight);
    
    // hotPosts
    if (_hotPostsView) {
        _hotPostsView.frame = CGRectMake(kMarginLeft, CGRectGetMaxY(_newestHotLabel.frame) + 3, self.frame.size.width - kMarginLeft *2, kPostsViewHeight);
    }
    
    if (_hotPostsView2) {
        _hotPostsView2.frame = CGRectMake(kMarginLeft, CGRectGetMaxY(_hotPostsView.frame), self.frame.size.width - kMarginLeft *2, kPostsViewHeight);
    }
    
}

- (void) initHotPostsViewWithReviews:(NSArray *)reviews{
    if (_hotPostsView == nil) {
        // 第一条
//        YSHotPostsView *hotPostsView = [YSHotPostsView hotPostView];
        YSHotPostsView *hotPostsView = [[YSHotPostsView alloc] init];
        
        hotPostsView.hidden = YES;
        hotPostsView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:hotPostsView];
        _hotPostsView = hotPostsView;
        
//        NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:hotPostsView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.f];
//        
//        [self addConstraint:constraint];
//        
//        constraint = [NSLayoutConstraint constraintWithItem:hotPostsView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.f];
//        
//        [self addConstraint:constraint];
//        
//        
//        constraint = [NSLayoutConstraint constraintWithItem:hotPostsView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_newestHotLabel attribute:NSLayoutAttributeTop multiplier:1.0f constant:3.f];
//        
//        [self addConstraint:constraint];
        
        
        // 第二条
//        YSHotPostsView *hotPostsView1 = [YSHotPostsView hotPostView];
        YSHotPostsView *hotPostsView1 = [[YSHotPostsView alloc] init];
        hotPostsView1.hidden = YES;
        hotPostsView1.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:hotPostsView1];
        _hotPostsView2 = hotPostsView1;
        
//        constraint = [NSLayoutConstraint constraintWithItem:hotPostsView1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.f];
//        
//        [self addConstraint:constraint];
//        
//        constraint = [NSLayoutConstraint constraintWithItem:hotPostsView1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.f];
//        
//        [self addConstraint:constraint];
//        
//        
//        constraint = [NSLayoutConstraint constraintWithItem:hotPostsView1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:hotPostsView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.f];
//        
//        [self addConstraint:constraint];
        
    }
   
    if (!reviews.count) {
        _hotPostsView.hidden = YES;
        _hotPostsView2.hidden = YES;
        return;
    }
     _hotPostsView.hidden = NO;
    _hotPostsView.groupReview = reviews[0];
    
    if (reviews.count == 1) {
        _hotPostsView2.hidden = YES;
        return;
    }
    _hotPostsView2.hidden = NO;
    _hotPostsView2.groupReview = reviews[1];
}

//-(void) setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
//{
//    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(10.0, 10.0)];
//    
//    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
//    [shape setPath:rounded.CGPath];
//    
//    view.layer.mask = shape;
//}

@end
